﻿using Domain;
using Domain.Entity.Config;
using Domain.Repository.IConfig;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repository.Config
{
    public class ERRepository : IEngage_resumeRepository
    {
        private readonly MyDbContext myDbContext;

        public ERRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }
        public async Task<bool> AddERAsync(Engage_resume er)
        {
            er.Check_status = 1;
            myDbContext.engage_Resumes.Add(er);
            int result= await myDbContext.SaveChangesAsync();
            return result > 0;
        }
        public ERFenYE DateERSSAsync(string Human_major_kind_id, string Human_major_id, string gzc, DateTime startDate, DateTime endDate, int page, int pageSize, int zt, int zt1)
        {
            var query=myDbContext.engage_Resumes.AsQueryable();
            if (!string.IsNullOrEmpty(Human_major_kind_id))
            {
                query = query.Where(s => s.Human_major_kind_id == Human_major_kind_id);
            }

            
            if (!string.IsNullOrEmpty(Human_major_id))
            {
                query = query.Where(s => s.Human_major_id == Human_major_id);
            }
            
            
            if (!string.IsNullOrEmpty(gzc))
            {
                query = query.Where(s => s.Human_name.Contains(gzc));
            }
           
            
            if (startDate !=default && endDate !=default)
            {
                query = query.Where(s => s.Regist_time >= startDate && s.Regist_time <= endDate);
            }
            
            
            if (!string.IsNullOrEmpty (zt.ToString()))
            {
                query = query.Where(s => s.Check_status == zt);
            }
            
            
            if (!string.IsNullOrEmpty (zt1.ToString()))
            {
                query = query.Where(s=>s.Interview_status == zt1);
            }
            

            int totaRecords = query.Count();
            var ER = query.Skip((page - 1)*pageSize).Take(pageSize).ToList();
            return new ERFenYE
            {
                totalRecords = totaRecords,
                er = ER
            };
        }

        public ERFenYE DateERSAsync(int page, int pageSize, int zt)
        {
           var query = myDbContext.engage_Resumes.AsQueryable();
            if (!string.IsNullOrEmpty(zt.ToString()))
            {
                query = query.Where(s => s.Interview_status == zt);
            }
            int totaRecords = query .Count();
            var ER=query.Skip((page - 1)*pageSize).Take(pageSize).ToList();
            return new ERFenYE
            {
                totalRecords = totaRecords,
                er = ER
            };
        }

        public async Task<bool> DeleteERAsync(int Id)
        {
            Engage_resume er = new Engage_resume()
            {
                Res_id = Id
            };
            myDbContext.engage_Resumes.Remove(er);
            int changes = await myDbContext.SaveChangesAsync();
            return changes > 0;
        }
        public async Task<Engage_resume> GetERByNameAsync(int Id)
        {
            return await myDbContext.engage_Resumes.Where(e => e.Res_id == Id).FirstOrDefaultAsync();
        }

        public async Task<bool> UpdateERAsync(Engage_resume er)
        {
            er.Interview_status = 1;
            er.Check_status = 2;
            myDbContext.engage_Resumes.Update(er);
            return await myDbContext.SaveChangesAsync()>0;
        }


        /// <summary>
        /// 录用申请修改
        /// </summary>
        /// <param name="er"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<bool> UpdateERApplicationAsync(Engage_resume er)
        {
            Engage_resume erl = await myDbContext.engage_Resumes.Where(e => e.Res_id == er.Res_id).FirstOrDefaultAsync();
            erl.Interview_status = er.Interview_status;
            erl.Pass_checkComment = er.Pass_checkComment;
            myDbContext.engage_Resumes.Update(erl);
            return await myDbContext.SaveChangesAsync()>0;
        }



        public async Task<bool> UpdateERSAsync(Engage_resume er)
        {
            Engage_resume erl = await myDbContext.engage_Resumes.Where(e => e.Res_id == er.Res_id).FirstOrDefaultAsync();
            erl.Interview_status = er.Interview_status;
            myDbContext.engage_Resumes.Update(erl);
            return await myDbContext.SaveChangesAsync()>0;
        }


        /// <summary>
        /// 录用审批修改
        /// </summary>
        /// <param name="er"></param>
        /// <returns></returns>
        public async Task<bool> UpdateERApprovalAsync(Engage_resume er)
        {
            Engage_resume erl = await myDbContext.engage_Resumes.Where(e => e.Res_id == er.Res_id).FirstOrDefaultAsync();
            erl.Interview_status = er.Interview_status;
            erl.Pass_checkComment = er.Pass_checkComment;
            erl.Pass_passComment = er.Pass_passComment;
            myDbContext.engage_Resumes.Update(erl);
            return await myDbContext.SaveChangesAsync()>0;
        }



        public ERFenYE DateERAsync(string Human_major_kind_id, string Human_major_id, string gzc, DateTime startDate, DateTime endDate, int page, int pageSize, int zt)
        {
           var query = myDbContext.engage_Resumes.AsQueryable();
            if (!string .IsNullOrEmpty(Human_major_kind_id))
            {
                query = query.Where(s => s.Human_major_kind_id == Human_major_kind_id);
            }
            if (!string.IsNullOrEmpty(Human_major_id))
            {
                query = query.Where(s => s.Human_major_id == Human_major_id);
            }


            if (!string.IsNullOrEmpty(gzc))
            {
                query = query.Where(s => s.Human_name.Contains(gzc));
            }


            if (startDate != default && endDate != default)
            {
                query = query.Where(s => s.Regist_time >= startDate && s.Regist_time <= endDate);
            }


            if (!string.IsNullOrEmpty(zt.ToString()))
            {
                query = query.Where(s => s.Check_status == zt);
            }


            int totaRecords = query.Count();
            var ER = query.Skip((page - 1) * pageSize).Take(pageSize).ToList();
            return new ERFenYE
            {
                totalRecords = totaRecords,
                er = ER
            };
        }
    }
}
